package com.zretc.support.controller;


import com.zretc.support.entity.*;
import com.zretc.support.service.ExamPaperService;
import com.zretc.support.service.serviceimpl.AnswerServiceImpl;
import com.zretc.support.service.serviceimpl.ScoreServiceImpl;
import com.zretc.support.tool.AjaxResponse;
import com.zretc.support.tool.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/api")
public class ScoreController {
    @Autowired
    ScoreServiceImpl ssi;
    @Autowired
    AnswerServiceImpl asi;
    @Autowired
    ExamPaperService eps;

    /**
     * 根据学生编号、考试名称、学生名称查询+分页
     *
     * @param keywords
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "getScore", produces = "application/json;charset=utf-8")
    public PageData<Score> getScore(@RequestParam(value = "keywords", defaultValue = "") String keywords, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "6") int limit) {
        Page<Score> pd = ssi.selectByIdOrName(keywords, page, limit);
        PageData<Score> pageData = new PageData<Score>();
        pageData.setRecords(pd.getContent());
        pageData.setCode(0);
        pageData.setTotal((int) pd.getTotalElements());
        pageData.setSize(pd.getSize());
        pageData.setCurrent(pd.getNumber() + 1);
        pageData.setMsg("");
        System.out.println(pd.getContent());
        return pageData;
    }

    /**
     * 修改总成绩
     * @param score
     * @return
     */
    @GetMapping(value = "updTotalScore")
    public AjaxResponse updScore(@RequestParam("answerId") int answerId,
                                 @RequestParam(value = "score")double score,
                                 @RequestParam("examId")int examId,
                                 @RequestParam("userId")String userId,
                                 @RequestParam("scoreId")int scoreId){
        String msg = "修改成功！";
        int code = 200;
        int n = ssi.updScore(answerId,score,examId,userId,scoreId);
        if (n == 0) {
            msg = "修改失败！";
            code = 500;
        }
        AjaxResponse ar = new AjaxResponse(msg, code);
        return ar;
    }

    /**
     * 获取的是某场考试 某学生的 所有答题信息
     * @param examId
     * @param userId
     * @return
     */
    @GetMapping(value = "getAnswers", produces = "application/json;charset=utf-8")
    public List<AnsAndTop> getAnswers(@RequestParam("examId") int examId, @RequestParam("userId") String userId) {
        return asi.getAnswerMult(examId,userId);
    }

    /**
     *
     * @param keywords
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "getPart", produces = "application/json;charset=utf-8")
    public PageData<ScoreToPart> getPart(@RequestParam(value = "keywords", defaultValue = "") String keywords, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "6") int limit) {
        Page<ScoreToPart> pd = ssi.getPart(keywords, page, limit);
        PageData<ScoreToPart> pageData = new PageData<ScoreToPart>();
        pageData.setRecords(pd.getContent());
        pageData.setCode(0);
        pageData.setTotal((int) pd.getTotalElements());
        pageData.setSize(pd.getSize());
        pageData.setCurrent(pd.getNumber() + 1);
        pageData.setMsg("");
        return pageData;
    }
    /**
     * 获取的是某场考试 某学生的 所有答题信息
     * @param examId
     * @return
     */
    @GetMapping(value = "getScoreByExamId/{examId}", produces = "application/json;charset=utf-8")
    public List<Score> getScoreByExamId(@PathVariable("examId") int examId) {
        return ssi.getScoresByExamId((long)examId);
    }

    /**
     * 根据学生编号获取成绩
     * @param userId
     * @param examName
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "getScoreByStudentId")
    public PageData<Score> getScoreByUserId(@RequestParam(value = "userId") String userId,
                                            @RequestParam(value = "examName", defaultValue = "") String examName,
                                            @RequestParam(value = "page", defaultValue = "1") int page,
                                            @RequestParam(value = "size", defaultValue = "10") int size) {
        PageData<Score> pageData = new PageData<Score>();
        pageData = ssi.getScoreByUserId(userId, examName, page - 1, size);
        pageData.setCurrent(pageData.getCurrent() + 1);
        return pageData;
    }

    /**
     * 添加
     * @return
     */
    @GetMapping(value = "addScore/{userId}/{examId}")
    public AjaxResponse addScore(@PathVariable("examId")int examId, @PathVariable("userId")String userId){
        String msg = "添加成功！";
        int code = 200;
        Score score=new Score();
        Exam exam=new Exam();
        exam.setExamId((long)examId);
        score.setExam(exam);
        User user=new User();
        user.setUserId(userId);
        score.setStudent(user);
        score.setJoinTime(new Date());
        int n = ssi.addScore(score);
        if (n == 0) {
            msg = "添加失败！";
            code = 500;
        }
        AjaxResponse ar = new AjaxResponse(msg, code);
        return ar;
    }

    /**
     * 根据学生编号获得score
     *
     * @param keywords
     * @param page
     * @param limit
     * @return
     */
    @GetMapping(value = "getScore1", produces = "application/json;charset=utf-8")
    public PageData<Score> getScore1(@RequestParam(value = "userId") String userId,@RequestParam(value = "keywords", defaultValue = "") String keywords, @RequestParam(value = "page", defaultValue = "1") int page, @RequestParam(value = "limit", defaultValue = "6") int limit) {
        Page<Score> pd = ssi.getScoresByUserId(userId,keywords,page,limit);
        int count = 0;
        for (int i = 0; i < pd.getContent().size(); i++) {
            count = eps.countExampaperBypaperId(pd.getContent().get(i).getExam().getExampaper().getPaperId());
            pd.getContent().get(i).getExam().getExampaper().setCount(count);
        }
        PageData<Score> pageData = new PageData<Score>();
        pageData.setRecords(pd.getContent());
        pageData.setCode(0);
        pageData.setTotal((int) pd.getTotalElements());
        pageData.setSize(pd.getSize());
        pageData.setCurrent(pd.getNumber() + 1);
        pageData.setMsg("");
        System.out.println(pd.getContent());
        return pageData;
    }

}
